package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/userToList",method = {RequestMethod.GET,RequestMethod.POST})
    public ModelAndView test1(@RequestParam(value = "pageNum",required = false)Integer pageNum,
                              @RequestParam(value = "pageSize",required = false)Integer pageSize){
        //当前第几页
        Integer num = 1;
        //每页多少条
        Integer size = 5;

        //判断是不是第一次进入页面
        if(pageNum!=null&&!"".equals(pageNum)){
            num = pageNum;
        }
        if(pageSize!=null&&!"".equals(pageSize)){
            size = pageSize;
        }

        //设置分页的初始化参数
        PageHelper.startPage(num,size);
        //获取数据列表
        List<User> list = userService.selectUserAll();
        //加载分页工具 加入数据源
        PageInfo<User> pagehelper = new PageInfo<User>(list);

        ModelAndView mav = new ModelAndView("userList","pagehelper",pagehelper);
        return mav;
    }
}
